TEST12138

TEST12138

长路漫漫,唯心作伴。

# CSS Basics in Front-end Development

CSS Variables for Front-end Basics#

Let's demonstrate how to use them.

First, create a square.

f48c0316df908dfdc2376c0a92feb06.png

66509f7e0d7800c004ab8111bbe4d6e.png

We notice that the width and height are the same, and the code is repetitive.

Repetitive code will definitely cause inconvenience in maintenance. My code is relatively small, so you may not feel it, but when the code becomes larger, it will be very troublesome to maintain.

How do we write CSS variables? --variableName: value

For example:

--size: 300px;

When calling it later:

width: var(--size)

5a1528d3b666f5ad349c9c4d3aa01f4.png

When we want to change the width and height of the square, we just need to change the value of --size.

Variables can be inherited like CSS properties, so if there is a variable that needs to be used throughout the webpage, it can be written in HTML or .

Since it is a variable, we can also participate in calculations.

To make it look convenient, let's create a child div.

a9956602f2bc1edbcba5db873bcffa2.png

We want to make the padding of the box 20% of the width, so we can write it like this:

padding:calc(var(--size) * 0.2)

d5be5b2236a77bfe781f69addfef281.png

Make the width and height of the child element half of the parent element.

b6193e5ae8af1a1ea4ad5b01233251f.png

85af79c07aca3b7644e174c89ba25d4.png

Change the parent element to 100px.

fe8ecba7808dc6454bdd2ca261bcce5.png

No problem.

Used to write global themes.

14e830a491425609f0ee870b29ec706.png

478734ec240b1133ece0767311f8452.png

Although the theme color is the same, the transparency is different in different places.

Change the transparency of the theme color. Use three RGB values as variables, and adjust the transparency with RGBA. If you want to preview the color, you can use comments.

3d3ea64fc7ea2aae70a73fd6024cc9d.png

f5d9a6362aad8f8ba823a4fbb481de6.png

If multiple elements have the same animation but different sizes, so the offset is also different, then you need to write multiple animations.

Just set a CSS variable on each element and call it in the animation.

<div class="box" style="--dotsize: 10px"></div>
<div class="box" style="--dotsize: 5px"></div>
transform:translate(calc(var(--dotsize) * 3),calc(var(--dotsize) - 3))

Okay, that's all for now. Explore more usage on your own and I wish you all become proficient in CSS soon.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.